var gulp = require("gulp");
var cssnano = require("gulp-cssnano");
var rename = require("gulp-rename");
var uglify = require("gulp-uglify");
var concat = require("gulp-concat");
var bs = require("browser-sync").create();
var watch = require("gulp-watch");
var sass = require("gulp-sass");
var util = require("gulp-util");
var sourcemaps = require("gulp-sourcemaps");

var path = {
    'html':'./templates/**/',
    'css':'./src/css/**/',
    'js':'./src/js/',
    'images':'./src/images/',
    'css_dist':'./dist/css/',
    'js_dist':'./dist/js/',
    'images_dist':'./dist/images',
};

//定义处理html的文件
gulp.task("html", function () {
    gulp.src(path.html + '*.html')
        .pipe(bs.stream())
});

//定义一个处理css的任务
gulp.task("css", function () {
    gulp.src(path.css + '*.scss')
        .pipe(sass().on("error",sass.logError))
        .pipe(cssnano())
        .pipe(rename({"suffix":".min"}))
        .pipe(gulp.dest(path.css_dist))
        .pipe(bs.stream())
});

//定义一个处理js的任务
gulp.task("js", function () {
    gulp.src(path.js + "*.js")
        .pipe(sourcemaps.init())
        .pipe(uglify().on('error',util.log))
        .pipe(rename({"suffix":".min"}))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(path.js_dist))
        .pipe(bs.stream())
});

//定义处理图片的任务
gulp.task("images", function () {

});

//定义监听文件修改
gulp.task("watch", function () {
    watch(path.css + "*.scss", gulp.parallel(['css']));
    watch(path.html + "*.html", gulp.parallel(['html']));
    watch(path.js + "*.js", gulp.parallel(['js']));
    watch(path.images + "*.*", gulp.parallel(['images']));
});

//初始化browser-sync的任务
gulp.task("bs", function () {
    bs.init({
        'server':{
            'baseDir':'./'
        }
    });
});

//创建一个默认的任务
// gulp.task("default", gulp.parallel(['bs', 'watch']));
gulp.task("default", gulp.parallel(['watch']));